package com.example.score.utils;

import com.example.score.entity.Message;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketMessage;

/**
 * @author fsyj
 */
public class JsonSocketMessageUtil {
    public static WebSocketMessage<String> wrapMessage(Object message) {
        return new TextMessage(JsonUtil.obj2String(message));
    }

    public static Message parseMessageToJson(WebSocketMessage<?> message) {
        Object payload = message.getPayload();
        Message msg = null;
        if (payload instanceof CharSequence) {
            String body = (String) payload;
            msg = JsonUtil.jsonToObject(body, Message.class);
        }
        return msg;
    }
}
